<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\index\logic;

use app\model\Supplies;
use app\model\SuppliesApply;

use app\model\SuppliesApplyList;
use app\model\Warehouse;
use think\facade\Db;
use think\facade\Request;

class ApplyLogic
{
    public static function getList()
    {
        $param = Request::get();
        $orgId = config('user')['orgid'];
        $uid = config('user')['id'];

        $where = [
            'sa_orgid' => $orgId,
            'sa_uid' => $uid,
        ];

        $applyModel = new SuppliesApply();
        $list = $applyModel->getApplyListByCondition($where);
        return $list;
    }

    public static function add()
    {
        $param = Request::post();

        $orgId = config('user')['orgid'];
        $uid = config('user')['id'];

        $apply = [
            'sa_orgid' => $orgId,
            'sa_uid' => $uid
        ];
        if(!isset($param['floor']) || empty($param['floor']))   fail('请选择楼层');
        $apply['sa_floor'] = trim($param['floor']);

        if(!isset($param['applys']) || empty($param['applys']) || !is_array($param['applys']))   fail('请选择申领的用品');
        $suppliesModel = new Supplies();
        $suppliesApplyModel = new SuppliesApply();
        $warehouseModel = new Warehouse();

        Db::startTrans();
        $applyId = $suppliesApplyModel->addOneSuppliesApply($apply);
        if(!$applyId){
            Db::rollback();
            fail('申领信息添加失败，请稍后重试');
        }
        $list = $stock = [];
        foreach ($param['applys'] as $apply){
            if(isset($apply['suppliesId']) && isset($apply['num'])){
                $suppliesId = intval($apply['suppliesId']);
                $suppliesInfo = $suppliesModel->getSuppliesInfoById($suppliesId);
                if(!$suppliesInfo)  fail('申领的用品不存在');

                //判断库存
                $checkStockWhere = [
                    'w_supplies_id' => $suppliesId,
                    'w_status' => 1,
                    'w_orgid' => $orgId
                ];
                $num = intval($apply['num']);
                $warehouseInfo = $warehouseModel->getWarehouseInfoByWhere($checkStockWhere,'w_id,w_stock');

                if(!$warehouseInfo || !isset($warehouseInfo['w_stock']) || $warehouseInfo['w_stock'] < $num){
                    Db::rollback();
                    fail($suppliesInfo['name'].'库存不足，暂时不能申领');
                }

                $list[] = [
                    'sal_apply_id' => $applyId,
                    'sal_supplies_id' => $suppliesId,
                    'sal_num' => $num,
                    'sal_create_time' => time()
                ];
                $stock[] = [
                    'w_id' => $warehouseInfo['w_id'],
                    'w_stock' => $warehouseInfo['w_stock'] - $num
                ];
            }
        }

        if(!$list){
            Db::rollback();
            fail('申领列表不能为空');
        }

        $applyListModel = new SuppliesApplyList();
        $cnt = $applyListModel->addSuppliesApplyList($list);

        if(!$cnt){
            Db::rollback();
            fail('申领列表添加失败，请稍后重试');
        }

        //减少库存
        if($stock){
            $rt = $warehouseModel->saveAll($stock);
            if(!$rt){
                Db::rollback();
                fail('库存减少失败，请稍后重试');
            }
        }

        Db::commit();
        return true;
    }

}